<template>
  <div class="container">
    <!-- 头部面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>广告</el-breadcrumb-item>
      <el-breadcrumb-item>广告位</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-button type="primary" @click="addAdvDialogVisible = true">添加广告位</el-button>
      <el-table :data="advList" border>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="name" label="名称"></el-table-column>
        <el-table-column label="操作" width="200">
          <template v-slot="scope">
            <el-button
              type="primary"
              size="mini"
              round
              icon="el-icon-edit"
              @click="showEditAdvDialog(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              round
              icon="el-icon-delete"
              @click="removeAdvById(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加广告位弹出框 -->
    <el-dialog
      title="添加广告位"
      :visible.sync="addAdvDialogVisible"
      width="50%"
      @close="addAdvDialogClose"
    >
      <el-form :model="addAdvForm" :rules="addAdvFormRules" ref="addAdvFormRef" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="addAdvForm.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addAdvDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addAdv">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑广告位弹出框 -->
    <el-dialog
      title="编辑广告位"
      :visible.sync="editAdvDialogVisible"
      width="50%"
      @close="editAdvDialogClose"
    >
      <el-form
        :model="editAdvForm"
        :rules="editAdvFormRules"
        ref="editAdvFormRef"
        label-width="80px"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="editAdvForm.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editAdvDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editAdv">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Adv',
  data() {
    return {
      // 广告位列表
      advList: [],
      // 控制添加广告位弹出框
      addAdvDialogVisible: false,
      // 添加广告位的表单对象
      addAdvForm: {
        name: ''
      },
      // 添加广告位的表单验证规则对象
      addAdvFormRules: {
        name: [{ required: true, message: '请输入广告位名称', trigger: 'blur' }]
      },
      // 控制编辑广告位弹出框
      editAdvDialogVisible: false,
      // 编辑广告位的表单对象
      editAdvForm: {
        id: '',
        name: ''
      },
      // 编辑广告位的表单验证规则对象
      editAdvFormRules: {
        name: [{ required: true, message: '请输入广告位名称', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getAdvList()
  },
  methods: {
    // 获取广告位列表
    async getAdvList() {
      const { data: res } = await this.$http.get('/admin/adv')
      this.advList = res.data
    },
    // 监听添加广告位弹出框的关闭事件
    addAdvDialogClose() {
      this.$refs.addAdvFormRef.resetFields()
    },
    // 添加广告位
    addAdv() {
      this.$refs.addAdvFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('/admin/adv', this.addAdvForm)
        if (res.status !== 200) return this.$message.error(res.msg)
        this.$message.success(res.msg)
        // 关闭弹出框
        this.addAdvDialogVisible = false
        // 重新获取数据
        this.getAdvList()
      })
    },
    // 显示编辑广告位弹出框
    async showEditAdvDialog(row) {
      const { data: res } = await this.$http.get(`/admin/adv/${row.id}`)
      if (res.status !== 200) return this.$message.error(res.msg)
      this.editAdvForm = res.data
      this.editAdvDialogVisible = true
    },
    // 监听编辑广告位弹出框关闭事件
    editAdvDialogClose() {
      this.$refs.editAdvFormRef.resetFields()
    },
    // 修改广告位
    editAdv() {
      this.$refs.editAdvFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put(`/admin/adv/${this.editAdvForm.id}`, {
          name: this.editAdvForm.name
        })
        if (res.status !== 200) return this.$message.error(res.msg)
        this.$message.success(res.msg)
        // 关闭弹出框
        this.editAdvDialogVisible = false
        // 重新获取数据
        this.getAdvList()
      })
    },
    // 根据id删除广告位
    removeAdvById(row) {
      this.$confirm('请确定是否删除广告位', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(`/admin/adv/${row.id}`)
          if (res.status !== 200) return this.$message.error(res.msg)
          this.$message.success(res.msg)
          // 重新获取数据
          this.getAdvList()
        })
        .catch(() => this.$message.info('已取消删除'))
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin-top: 20px;
}
</style>
